package com.design.prototype;

import java.io.IOException;
import java.util.Objects;

/**
 * @author leali
 * @title: PrototypeClient
 * @projectName design
 * @description: PrototypeClient
 * @date 2022/1/2 20:31
 */
public class PrototypeClient {
    public static void main(String[] args) {
        WeeklyLog weeklyLog = new WeeklyLog();
        weeklyLog.setName("张无忌");
        weeklyLog.setDate("第1周");
        weeklyLog.setContent("这周工作很忙，每天加班");
        System.out.println("****周报****");
        System.out.println("周次：" + weeklyLog.getDate());
        System.out.println("姓名：" + weeklyLog.getName());
        System.out.println("内容：" + weeklyLog.getContent());
        System.out.println("--------------------------------");
        //调用克隆方法创建克隆对象
        WeeklyLog newWeeklyLog = weeklyLog.clone();
        newWeeklyLog.setDate("第2周");
        System.out.println("****周报****");
        System.out.println("周次：" + newWeeklyLog.getDate());
        System.out.println("姓名：" + newWeeklyLog.getName());
        System.out.println("内容：" + newWeeklyLog.getContent());

        //测试深浅拷贝
        Attachment attachment = new Attachment();
        attachment.setName("测试");
        weeklyLog.setAttachment(attachment);
        WeeklyLog weeklyLog1 = weeklyLog.clone();
        System.out.println("浅拷贝");
        System.out.println("周报是否相同-" + (weeklyLog == weeklyLog1));
        System.out.println("附件是否相同-" + (weeklyLog.getAttachment() == weeklyLog1.getAttachment()));
        System.out.println("深拷贝");
        WeeklyLog weeklyLog2 = null;
        try {
            weeklyLog2 = weeklyLog.deepClone();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("周报是否相同-" + (weeklyLog == weeklyLog2));
        System.out.println("附件是否相同-" + (weeklyLog.getAttachment() == Objects.requireNonNull(weeklyLog2).getAttachment()));
    }
}
